<template>
	<view>
		<view class="friBtn">
			<u-button v-if="isFriend" type="info">已经是好友</u-button>
			<u-button @click="addFri" v-else type="info">添加好友</u-button>
		</view>


		<view class="">
			<view class="list">
				<view class="list-item list-nic">
					<text class="name">昵称：</text>{{detail.nickname}}
				</view>

				<view class=" list-item list-pic">
					<text class="name">头像：</text>
					<u-avatar style="relative: absolute; margin-left: 30rpx;" :src="detail.imgSrc" size="160"
						mode="square"></u-avatar>
				</view>

				<view class="list-item list-sig">
					<text class="name">个性签名：</text>
					{{detail.signature}}
				</view>

				<view class="list-item list-pho">
					<text class="name">手机号：</text>
					{{detail.phoneNumber}}
					<u-icon @click="callPho" style="position: relative; left:30rpx;" size="50" color="#71d5a1"
						name="phone"></u-icon>
				</view>

				<view class="list-item list-wx">
					<text class="name">微信号：</text>
					{{detail.wxNumber}}
					<u-icon @click="past" style="position: relative; left:30rpx;" size="50" color="#71d5a1"
						name="file-text-fill"></u-icon>
					<u-toast ref="uToast" />
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				detail: {},
				isFriend: false
			}
		},

		onLoad(options) {
			var that = this
			let id = options.userid
			const db = uniCloud.database()
			db.collection('users').doc(id).get().then(res => {
				// console.log(res.result.data[0])
				this.detail = res.result.data[0]
				// this.detail = Object.assign(this.detail, res.result.data[0])

			})
		},

		methods: {
			callPho() {
				uni.makePhoneCall({
					phoneNumber: this.detail.phoneNumber
				});
			},

			past() {
				var that = this
				uni.setClipboardData({
					data: this.detail.wxNumber,
					showToast: true,
					success: function() {
						// that.$refs.uToast.show({
						// 	title: '复制成功',
						// 	type: 'success',
						// })
					}
				});
			},
			
			
			
			

			addFri() {
				const db = uniCloud.database()
				const dbCmd = db.command
				if (getApp().globalData.user._id) {
					db.collection('message').where({
						userId:this.detail._id
					}).get().then(res=>{
						if (res.result.data.length) {
							if ( res.result.data[0].list.includes(getApp().globalData.user._id) ) {
								uni.showToast({
									title:'已经申请过'
								})
							} else{
								
								uniCloud.callFunction({
									name:'uplikes',
									data:{
										collection: 'message',
										// doc: res.result.data[0].userId,
										where:{
											userId : this.detail._id
										},
										
										data : `{list : dbCmd.unshift(['${getApp().globalData.user._id}'])}`
									}
								}).then(res=>{
									uni.showToast({
										title:'申请成功~'
									})
								}).catch(err=>{
									console.log(err)
								})
								
								
							}
							
						} else{
							db.collection('message').add({
									userId : this.detail._id,
									list : [getApp().globalData.user._id]
							}).then(res=>{
								uni.showToast({
									title:'申请成功'
								})
							})
						}
					})
				} else {
					uni.showToast({
						title: "请先登录！",
						duration: 2000,
						icon: 'none',
						success() {
							setTimeout( ()=> {
								uni.switchTab({
									url: '/pages/user/user'
								})
							}, 2000)

						}
					})
				}
			},
		}
	}
</script>

<style>
	.name {
		font-size: 45rpx;
		color: #fcbd71;
	}

	.list {
		font-size: 35rpx;
	}

	.list-item {
		width: 100%;
		height: 190rpx;
		border: 3px #e4e7ed solid;
		display: flex;
		align-items: center;
	}
</style>
